﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace Jbe.TestSuite.Infrastructure.Interface.Documents
{
    public abstract class DocumentType
    {
        private readonly string description;
        private readonly string fileExtension;


        protected DocumentType(string description, string fileExtension)
        {
            this.description = description;
            this.fileExtension = fileExtension;
        }


        public string Description { get { return description; } }

        public string FileExtension { get { return fileExtension; } }


        public virtual bool CanNew() { return false; }

        public Document New()
        {
            if (!CanNew()) { throw new NotSupportedException("The New operation is not supported. CanNew returned false."); }
            return NewCore();
        }

        public virtual bool CanOpen() { return false; }

        public Document Open(string fileName)
        {
            if (!CanOpen()) { throw new NotSupportedException("The Open operation is not supported. CanOpen returned false."); }
            Document document = OpenCore(fileName);
            document.FileName = fileName;
            return document;
        }

        public virtual bool CanSave(Document document) { return false; }

        public void Save(Document document, string fileName)
        {
            if (!CanSave(document)) { throw new NotSupportedException("The Save operation is not supported. CanSave returned false."); }
            SaveCore(document, fileName);
            if (CanOpen()) { document.FileName = fileName; }
        }

        protected virtual Document NewCore() { return null; }

        protected virtual Document OpenCore(string fileName) { return null; }

        protected virtual void SaveCore(Document document, string fileName) { }
    }
}
